//
//  HJUITabBarController.m
//  12期传智微博HJ
//
//  Created by mac on 15-1-28.
//  Copyright (c) 2015年 jun. All rights reserved.
//

#import "HJUITabBarController.h"
#import "HJTabBar.h"
#import "HJHomeViewController.h"
#import "HJDiscoverController.h"
#import "HJMessageViewController.h"
#import "HJProfileViewController.h"
#import "HJNavigationController.h"


@interface HJUITabBarController ()<HJTabBarDelegate>
/**
 /自定义TabBar
 */
@property (nonatomic, weak) HJTabBar *customTabBar;
@end

@implementation HJUITabBarController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        //创建子控制器
        
        UIViewController *home = [self addControllerWithClass:[HJHomeViewController class] title:@"首页" image:@"tabbar_home" selectedImg:@"tabbar_home_selected"];
        UIViewController *message = [self addControllerWithClass:[HJMessageViewController class] title:@"消息" image:@"tabbar_message_center" selectedImg:@"tabbar_message_center_selected"];
        
        UIViewController *discover = [self addControllerWithClass:[HJDiscoverController class] title:@"发现" image:@"tabbar_discover" selectedImg:@"tabbar_discover_selected"];
        UIViewController *profile = [self addControllerWithClass:[HJProfileViewController class] title:@"我" image:@"tabbar_profile" selectedImg:@"tabbar_profile_selected"];
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建自定义的HJTabBar
    HJTabBar *tabBar = [[HJTabBar alloc]init];
    
    tabBar.delegate = self;
    
    tabBar.frame = self.tabBar.frame;
    [self.view addSubview:tabBar];
    
    self.customTabBar =tabBar;
    
    [self.tabBar removeFromSuperview];


}
/**
 *  根据一个类创建一个控制器
 *
 *  @param class         控制器对应的类
 *  @param title         标题
 *  @param image         默认状态的图片
 *  @param selectedImage 选择
 *
 *  @return 创建号的控制器
 */
- (UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImg:(NSString *)selectedImage
{
    UIViewController *vc = [[class alloc]init];
    return [self addControllerWithVc:vc title:title image:image selectedImg:selectedImage];
}
/**
 *  根据一个创建好的控制器初始化控制器
 *
 *  @param vc            控制器
 *  @param title         标题
 *  @param image         默认图片
 *  @param selectedImage 选中图片
 *
 *  @return 初始化之后的控制器
 */
- (UIViewController *)addControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImg:(NSString *)selectedImage
{
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    /**
     *  iOS7需要通过代码告诉系统不用渲染图片
     
     */
    //创建图片
    UIImage *newImage =[UIImage imageNamed:selectedImage];
    //原样显示
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置图片
    vc.tabBarItem.image = newImage;

    //vc.view.backgroundColor = HJRandomColor;
    //设置标题颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
#warning mark - 别忘记包装导航控制器
    HJNavigationController *nav = [[HJNavigationController alloc]initWithRootViewController:vc];
    //添加控制器
    [self addChildViewController:nav];
    
    // 每添加一个子控制器就要创建一个对应的按钮
    [self.customTabBar addItem: vc.tabBarItem];

    return vc;
    
    
    
}
#pragma mark - CZTabBarDelegate
- (void)tabBar:(HJTabBar *)tabBar selectedBtnFrom:(NSInteger)from to:(NSInteger)to
{
  
    // 切换控制器
    // 1.取出当前选中按钮对应的控制器
    //    UIViewController *vc = self.childViewControllers[to];
    //    self.selectedViewController = vc;
    
    self.selectedIndex = to;
}

@end
